package com.ceba.test.modules.admin.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.ceba.cebautils.StringUtil;
import org.json.JSONObject;

import com.ceba.base.web.dao.IBaseDao;
import com.ceba.test.modules.admin.entity.DirManager;

import abc.ID;
import abc.data.DataRow;
import abc.data.DataTable;
import abc.data.Sql;
import abc.data.SqlSessionManager;
import abc.data.dataSource.SqlDataSource;
import abc.data.store.IStore;
import abc.data.store.StoreManager;
import abc.object.XObject;

public class DirManagerDao  extends IBaseDao{
	
	
	public  DataTable getDataTableById(String Id) throws Exception{
		Sql sql  = new Sql("SELECT ID,DIR_PATH,TEMP_DIR_PATH,SAVE_DIR_PATH,FAIL_DIR_PATH FROM dirmanagers where ID='"+Id+"'");
		
		return SqlSessionManager.getDataTable(sql);

	}
	
	public  DataTable getDataTableByDir(String dirPath) throws Exception{
		Sql sql  = new Sql("SELECT ID,DIR_PATH,TEMP_DIR_PATH,SAVE_DIR_PATH,FAIL_DIR_PATH FROM dirmanagers where DIR_PATH='"+dirPath+"'");
		
		return SqlSessionManager.getDataTable(sql);

	}
	
	public  DataTable getDataTable() throws Exception{
		Sql sql  = new Sql("SELECT ID,DIR_PATH,TEMP_DIR_PATH,SAVE_DIR_PATH,FAIL_DIR_PATH FROM dirmanagers ");
		
		return SqlSessionManager.getDataTable(sql);

	}
	public  DirManager getDirById(String ID)throws Exception{
		DirManager dm = new DirManager();
		DataTable dt = getDataTableById(ID);
		if(dt !=null && dt.getRowCount()>0){
			DataRow dr = dt.getRow(0);
			dm.setId(ID);
			dm.setDirPath(dr.getString("DIR_PATH"));
			dm.setSaveDirPath(dr.getString("SAVE_DIR_PATH"));
			dm.setTempDirPath(dr.getString("TEMP_DIR_PATH"));
			dm.setFailDirPath(dr.getString("FAIL_DIR_PATH"));
			return dm;
		}else{
			return null;
		}
		
	}
	
	public  DirManager getDirByDirPath(String dirPath) throws Exception{
		DirManager dm = new DirManager();
		DataTable dt = getDataTableByDir(dirPath);
		if(dt !=null && dt.getRowCount()>0){
			DataRow dr = dt.getRow(0);
			dm.setId(dr.getString("ID"));
			dm.setDirPath(dr.getString("DIR_PATH"));
			dm.setSaveDirPath(dr.getString("SAVE_DIR_PATH"));
			dm.setTempDirPath(dr.getString("TEMP_DIR_PATH"));
			dm.setFailDirPath(dr.getString("FAIL_DIR_PATH"));
			return dm;
		}else{
			return null;
		}
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public  List<DirManager> getDirManagers() throws Exception{
		List<DirManager> dirs = new ArrayList();
		DataTable dt = getDataTable();
		if(dt !=null && dt.getRowCount()>0){
			for(int i = 0; i< dt.getRowCount(); i ++){
				DirManager dm = new DirManager();
				DataRow dr = dt.getRow(i);
				dm.setId(dr.getString("ID"));
				dm.setDirPath(dr.getString("DIR_PATH"));
				dm.setSaveDirPath(dr.getString("SAVE_DIR_PATH"));
				dm.setTempDirPath(dr.getString("TEMP_DIR_PATH"));
				dm.setFailDirPath(dr.getString("FAIL_DIR_PATH"));
				dirs.add(dm);
			}
		}
		return dirs;
	}

	/**
	 * 获取目录列表sql
	 * @return
	 */
	public JSONObject getDirList(String orderBy, String key,Map<String,Object> dic) throws Exception{
		if(StringUtil.isEmptyString(orderBy)){
			orderBy = "dirmanagers.UPDATE_TIME DESC";
		}
		Sql sql = null;
		StringBuffer ss = new StringBuffer("SELECT ID,DIR_PATH,TEMP_DIR_PATH,SAVE_DIR_PATH,FAIL_DIR_PATH FROM dirmanagers ");
		if(StringUtil.isEmptyString(key)){
			ss.append(" WHERE 1 = '1' ORDER BY " + orderBy);
			sql = new Sql(ss.toString());
		}else{
			ss.append(" WHERE DIR_PATH like '%"+key+"%' OR SAVE_DIR_PATH LIKE '%"+key+"%' ORDER BY " + orderBy);
			sql = new Sql(ss.toString());
		}
		SqlDataSource dataSource = new SqlDataSource();
		dataSource.getSelectCommands().add(sql);
		DataTable dt = dataSource.query(0, dic);
		if(dt !=null && dt.getRowCount() >0){
			return dt.toJSONObject();
		}else{
			return new JSONObject();
		}
	}

	/**
	 * 增加
	 * @param dir
	 * @return
	 */
	public boolean addDir(DirManager dir) {
		//id为空是新建记录
		IStore store = StoreManager.create("dirmanagers");
		XObject object = new XObject("ID",ID.getGuid());
		object.put("DIR_PATH", dir.getDirPath());
		object.put("TEMP_DIR_PATH", dir.getTempDirPath());
		object.put("SAVE_DIR_PATH", dir.getSaveDirPath());
		object.put("FAIL_DIR_PATH", dir.getFailDirPath());
		object.setUpdateUser();
		object.setUpdateTime();
		if(store.insert(object)>0){
			return true;
		}
		return false;
	}
	
	/**
	 * 更新
	 * @param dir
	 * @return
	 */
	public boolean updateDir(DirManager dir) {
		//id为空是新建记录
		IStore store = StoreManager.create("dirmanagers");
		XObject object = new XObject("ID",dir.getId());
		object.put("DIR_PATH", dir.getDirPath());
		object.put("TEMP_DIR_PATH", dir.getTempDirPath());
		object.put("SAVE_DIR_PATH", dir.getSaveDirPath());
		object.put("FAIL_DIR_PATH", dir.getFailDirPath());
		object.setUpdateUser();
		object.setUpdateTime();
		if(store.update(object)>0){
			return true;
		}
		return false;
	}
	

}
